package com.hee.erp.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.CaseFormat;
import com.hee.erp.constant.ConstDictType;
import com.hee.erp.modules.entity.*;
import com.hee.erp.modules.mapper.SysJobMapper;
import com.hee.erp.modules.service.ISysCompanyService;
import com.hee.erp.modules.service.ISysDepartService;
import com.hee.erp.modules.service.ISysDictValueService;
import com.hee.erp.modules.service.ISysJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hee.erp.vo.JobListPageParamsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weizehong
 * @since 2022-03-22
 */
@Service
@Transactional
public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob> implements ISysJobService {

    @Autowired
    ISysCompanyService companyService;

    @Autowired
    ISysDepartService departService;

    @Autowired
    ISysDictValueService dictValueService;

    @Override
    public IPage<SysJob> listByPage(JobListPageParamsVo vo) {

        IPage<SysJob> page = new Page<>(vo.getCurrent(), vo.getPageSize());
        page.setCurrent(vo.getCurrent());
        page.setPages(vo.getPageSize());

        QueryWrapper<SysJob> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(vo.getKey())) {
            //根据岗位、发布位置搜索
            queryWrapper.lambda().and(qw -> {
                qw.like(SysJob::getRoleName, vo.getKey())
                        .or().like(SysJob::getPublishSite, vo.getKey());
            });
        }
        if(vo.getDates() != null){
            //根据添加时间、截止时间搜索
            queryWrapper.lambda().and(qw -> {
                qw.like(SysJob::getCreateTime,vo.getDates())
                        .or().like(SysJob::getDeadline,vo.getDates()) ;
            });
        }

        if(StringUtils.isNotBlank(vo.getStatus())){
            //根据招聘状态搜索
            queryWrapper.lambda().and(qw -> {
                qw.like(SysJob::getStatus,vo.getStatus());
            });
        }


        this.page(page, queryWrapper);
        List<SysJob> records = page.getRecords();
        //设置字典显示name
        //招聘类型
        List<SysDictValue> jobTypeList = dictValueService.getListByTypeValue(ConstDictType.JOB_TYPE);
        //所属公司
        List<SysCompany> companyList = companyService.list();
        //所属部门
        List<SysDepart> departList = departService.list();
        //学历
        List<SysDictValue> educationList = dictValueService.getListByTypeValue(ConstDictType.EDUCATION);
        //招聘状态
        List<SysDictValue> statusList = dictValueService.getListByTypeValue(ConstDictType.JOB_STATUS);
        for (SysJob job : records) {
            //招聘类型
            if(job.getType() != null){
                SysDictValue dict = jobTypeList.stream()
                        .filter(dictValue -> dictValue.getDictValue().equals(job.getType()))
                        .findAny().get();
                job.setTypeName(dict.getDictName());
            }
            //所属公司
            if(StringUtils.isNotBlank(job.getCompanyId())){
                SysCompany sysCompany = companyList.stream()
                        .filter(company -> company.getId().equals(job.getCompanyId()))
                        .findAny()
                        .get();
                job.setCompanyName(sysCompany.getCompanyName());
            }
            //所属部门
            if(StringUtils.isNotBlank(job.getDepartId())){
                SysDepart sysDepart = departList.stream()
                        .filter(depart -> depart.getId().equals(job.getDepartId()))
                        .findAny()
                        .get();
                job.setDepartName(sysDepart.getDepartName());
            }
            //学历
            if(job.getEducation() != null){
                SysDictValue dict = educationList.stream()
                        .filter(dictValue -> dictValue.getDictValue().equals(job.getEducation()))
                        .findAny().get();
                job.setEducationName(dict.getDictName());
            }
            //招聘状态
            if(job.getStatus() != null){
                Optional<SysDictValue> any = statusList.stream()
                        .filter(dictValue -> dictValue.getDictValue().equals(job.getStatus()))
                        .findAny();
                if(any.isPresent()) {
                    job.setStatusName(any.get().getDictName());
                }
            }
        }
        return page;
    }

    @Override
    public SysJob getDetailById(String id) {
        SysJob entity = this.getById(id);
        if(entity != null){
            //设置公司名称
            if(StringUtils.isNotBlank(entity.getCompanyId())){
                SysCompany company = companyService.getById(entity.getCompanyId());
                if(company != null) {
                    entity.setCompanyName(company.getCompanyName());
                }
            }
            //设置部门名称
            if(StringUtils.isNotBlank(entity.getDepartId())){
                SysDepart depart = departService.getById(entity.getDepartId());
                if(depart != null) {
                    entity.setDepartName(depart.getDepartName());
                }
            }
            //设置招聘类型
            if(entity.getType() != null){
               SysDictValue dictValueEntity = dictValueService.getByTypeAndValue(ConstDictType.JOB_TYPE,entity.getType());
               if(dictValueEntity != null){
                   entity.setTypeName(dictValueEntity.getDictName());
               }
            }
            //设置学历
            if(entity.getEducation() != null){
                SysDictValue dictValueEntity = dictValueService.getByTypeAndValue(ConstDictType.EDUCATION,entity.getEducation());
                if(dictValueEntity != null){
                    entity.setEducationName(dictValueEntity.getDictName());
                }
            }
            //设置职位类型
            if(entity.getRoleType() != null){
                SysDictValue dictValueEntity = dictValueService.getByTypeAndValue(ConstDictType.JOB_ROLE_TYPE,entity.getRoleType());
                if(dictValueEntity != null){
                    entity.setRoleTypeName(dictValueEntity.getDictName());
                }
            }
        }
        return entity;
    }
}
